package com.huitong;

import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.jeecgframework.core.util.PropertiesUtil;

import com.diagcn.smslib.CIncomingMessage;
import com.diagcn.smslib.CMessage;
import com.diagcn.smslib.COutgoingMessage;
import com.diagcn.smslib.CStatusReportMessage;
import com.diagcn.smslib.SZHTOCService;

public class SmsService {
	private final static Logger  logger = Logger.getLogger(SmsService.class);
    public static void main(String [] args){
       System.out.println(sendSms("13223306650","测试接收！"));
    }
    /**
     * 发送短信
     * @param phone 手机号
     * @param content 内容
     * @return
     */
    public static String sendSms(String phone,String content) {
    	PropertiesUtil pu= new PropertiesUtil("smsConfig.properties");
        // 与短信设备建立连接，参数 1、端口号，2、速率，3、短信设备牌子，4、短信设备型号
        String port=pu.readProperty("port");
        int bote=pu.readProperty("bote")==""?9600:Integer.parseInt(pu.readProperty("bote"));
        String type=pu.readProperty("type");
        String other=pu.readProperty("other");
        SZHTOCService srv = new SZHTOCService(port, bote, type, other);

        try {
            // 设置短信中心号码
            //srv.setSmscNumber("+8613800571500");

            // 连接设备
            srv.connect();

            // 连接成功，可以显示短信设备状态
            logger.info("Mobile Device Information: ");
            logger.info("    Manufacturer  : "
                    + srv.getDeviceInfo().getManufacturer());
            logger.info("    Model         : "
                    + srv.getDeviceInfo().getModel());
            logger.info("    Serial No     : "
                    + srv.getDeviceInfo().getSerialNo());
            logger.info("    IMSI          : "
                    + srv.getDeviceInfo().getImsi());
            logger.info("    S/W Version   : "
                    + srv.getDeviceInfo().getSwVersion());
            logger.info("    Battery Level : "
                    + srv.getDeviceInfo().getBatteryLevel() + "%");
            logger.info("    Signal Level  : "
                    + srv.getDeviceInfo().getSignalLevel() + "%");

            // 创建发送对象
            COutgoingMessage msg = new COutgoingMessage(phone, content);

            // 设置编码
            msg.setMessageEncoding(CMessage.MessageEncoding.EncUcs2);

            // 此短信需要状态回复
            msg.setStatusReport(true);

            // 短信有效期
            msg.setValidityPeriod(8);

            // 发送短信
            srv.sendMessage(msg);

            // 接收短信代码====================================================================
            /*LinkedList<CIncomingMessage> msgList = new LinkedList<CIncomingMessage>();
            srv.readMessages(msgList, CIncomingMessage.MessageClass.All);
            for (int i = 0; i < msgList.size(); i++) {
                CIncomingMessage message = msgList.get(i);
                if (message instanceof CStatusReportMessage) {
                	logger.info(message);
                    // 此短消息为 状态回复短消息
                }
                srv.deleteMessage(message); // 删除都到的短信
            }*/
            // ==============================================================================

            srv.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        	return "0";
        }
        return "1";
    }
}